import 'package:flutter/material.dart';
import 'package:flutter_trip/model/common_model.dart';
import 'package:flutter_trip/model/grid_nav_model.dart';

class GridNav3 extends StatelessWidget {
  const GridNav3({Key? key, required this.gridNavModel}) : super(key: key);
  final GridNavModel gridNavModel;

  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
      color: Colors.white,
      borderRadius: BorderRadius.all(Radius.circular(7)),
      clipBehavior: Clip.antiAlias,

      child: Column(
        children: _gridItems(context),
      ),
    );
  }

  _gridItems(BuildContext context) {
    List<Widget> items = [];
    if (gridNavModel == null) {
      return items;
    }
    if (gridNavModel.hotel != null) {
      items.add(gridItem(context, gridNavModel.hotel , true));
    }
    if (gridNavModel.flight != null) {
      items.add(gridItem(context, gridNavModel.flight , false));

    }
    if (gridNavModel.travel != null) {
      items.add(gridItem(context, gridNavModel.travel , false));
    }

    return items;
  }

  gridItem(BuildContext context, GridNavItem item ,  bool first) {
    List<Widget> items = [];
    items.add(_main(context, item.mainItem));
    items.add(_double(context, item.item1, item.item2 ));
    items.add(_double(context, item.item3, item.item4));

    List<Widget> expendItems=[];

    items.forEach((item) {
      expendItems.add(Expanded(child: item, flex:1));
    });
    Color startColor = Color(int.parse('0xff'+item.startColor));
    Color endColor = Color(int.parse('0xff'+item.endColor));
    return Container(
      height: 88,
      margin:first ?null :  EdgeInsets.only(top: 3),
      decoration: BoxDecoration(
        //线性渐变
          gradient: LinearGradient(colors: [startColor ,endColor])
      ),
      child: Row(
        children: expendItems,
      ),
    );
  }

  _main(BuildContext context, CommonModel mainItem) {
    return Stack(
      alignment: AlignmentDirectional.topCenter,
      children: [
        Image.network(
          mainItem.icon!,
          width: 122,
          height: 88,
          alignment: AlignmentDirectional.bottomCenter,
        ),
        Padding(padding: EdgeInsets.only(top: 10),child: Text(
          mainItem.title!,
          style: TextStyle(fontSize: 14 ,color: Colors.white),
        ),)
      ],
    );
  }

  _double(BuildContext context, CommonModel item1, CommonModel item2) {
    return Column(
      children: [
        Expanded(
            flex: 1,
            child: _item(context , item1 , item2 , true )),
        Expanded(
            flex: 1,
            child: _item(context , item1 , item2 , false )),
      ],
    );
  }

  _item(BuildContext context, CommonModel item1, CommonModel item2, bool isFirst) {
    BorderSide borderSide = BorderSide(width: 0.8 , color: Colors.white);
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: Border(
            left: borderSide,
            bottom: isFirst? borderSide : BorderSide.none,
          )
        ),
        child: Text(
          item1.title!,
          style: TextStyle(fontSize: 13 , color: Colors.white),
        ),
      ),
    );
  }
}
